package com.example.weather.ui.notifications;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.weather.MainActivity;
import com.example.weather.MyDataHelper;
import com.example.weather.cityModel;
import com.example.weather.databinding.FragmentNotificationsBinding;
import com.example.weather.ui.dashboard.DashboardFragment;

public class NotificationsFragment extends Fragment {

    private FragmentNotificationsBinding binding;
    TextView add_city;
    Button qbtn;
    ListView view_all;
    MyDataHelper myDataHelper;
    Context context;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        NotificationsViewModel notificationsViewModel = new ViewModelProvider(this).get(NotificationsViewModel.class);
        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        final TextView textView = binding.textNotifications;
        notificationsViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        //入口
        Log.d("switchPage", "当前页面收藏");
        //初始化数据
        add_city=binding.addCity;
        qbtn=binding.btnQ;
        view_all=binding.viewAll;
        context = getContext();
        myDataHelper=new MyDataHelper(context);
        //先获取数据库数据列表
        viewAll();

        //查询按钮点击事件
        qbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cityModel citymodel=new cityModel(-1,add_city.getText().toString(),"off");
                myDataHelper=new MyDataHelper(context);
                String s=myDataHelper.addOne(citymodel);
                Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
                add_city.setText("");
                viewAll();
            }
        });

        //列表组件单个行点击事件
        view_all.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                cityModel citymodel= (cityModel) parent.getItemAtPosition(i);
                AlertDialog.Builder dialog=new AlertDialog.Builder(context);
                dialog.setTitle("请选择操作");
                dialog.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = myDataHelper.deleteOne(citymodel);
                        Toast.makeText(context,"DEL:"+s,Toast.LENGTH_SHORT).show();
                        viewAll();
                    }
                });
                dialog.setPositiveButton("显示天气", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(context, MainActivity.class);
                        intent.putExtra("id",citymodel.getId());
                        intent.putExtra("name",citymodel.getName());
                        intent.putExtra("collect",citymodel.getCollect());
                        startActivity(intent);
                    }
                });
                dialog.create();
                dialog.show();


            }
        });


        return root;
    }

    private void viewAll() {
        ArrayAdapter<cityModel> adapter =new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, myDataHelper.getAll());
        view_all.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}